/*
西游构造敌人专用类
*/
window._MCFC = {

    tempxwarr:[],

    _EM:{},

    _ELV:[5,50,100,200,300,400,500,600,700,800,900,1000],

    areaConfig:{
        "城内":1,
        "地府水":1,
        "地府土":1,
        "洞水":1,
        "洞土":1,
        "洞岩浆":1,
        "林":1,
        "民居":1,
        "水":1,
        "水底":1,
        "水底城内":1,
        "野区土":1, 
        "天宫":1,
        "天宫水":1,
        "天宫岩浆":1,
        "仙府":1,
        "佛土":1,
    },

    initTzData:function(tzchapter){

        var tzConf = _CTZ[tzchapter];

        var playerTeam = tzConf.player;

        //计算等级
        var armyLv = tzConf.lv;

        if(!tzConf.map){
            window._CHESSFIGHT.defaultMap = "地形_挑战通用";
        }else{
            window._CHESSFIGHT.defaultMap = tzConf.map;
        }

        //构造玩家
        for(var i=0;i<playerTeam.length;i++){
            var tempArmyType = playerTeam[i];
            var tempplayer = _MCFC.createNpcRole(tempArmyType,{lv:armyLv,nodz:1,nozm:1});
            tempplayer.isplayer = true;
            window.fightData.playerTeam.push(tempplayer);
        }
    
        //构造敌人
        window.fightData.armyTeam=[];
        for(var i=0;i<tzConf.army.length;i++){
            var tempArmyType = tzConf.army[i];
            window.fightData.armyTeam.push(
                _MCFC.createNpcRole(tempArmyType,{lv:armyLv,nodz:1,nozm:1})
            );
        }
    },

    initTaskData:function(areaname){

        if(window.gameData.workList
            &&window.gameData.workList[0]
            &&!window.gameData.workList[0].complete
            &&window.gameData.workList[0].map == window.fightData.chapter
        ){
            var taskObj = window.gameData.workList[0];
            //构造敌人
            var mapconf = _NPCMAP[window.fightData.chapter];
            //计算等级
            var armyLv = mapconf.lv;
            var dz = mapconf.elevel;
            window._CHESSFIGHT.defaultMap = "地形_BOSS战通用";
            //构造敌人
            window.fightData.taskName = taskObj.taskname;
            window.fightData.armyTeam=[];
            window.fightData.isTaskModel = 1;

            for(var i=0;i<taskObj.detail.length;i++){

                var targetObj = taskObj.detail[i];
                var armyNmb = targetObj.nmb?targetObj.nmb:1;
                var tempArmyType = targetObj.target;
                for(var j=0;j<armyNmb;j++){
                    window.fightData.armyTeam.push(
                        _MCFC.createNpcRole(tempArmyType,{lv:armyLv,dz:dz})
                    );
                }
            }
            return true;
        }

        return false;
    },

    initNormalData:function(areaname){

        //判断是否剧情战
        /*
        if(_MCFC.initTaskData(areaname)){
            console.log('是剧情战斗');
            return;
        }else{
            console.log('非剧情战斗');
        }
        */
        window.fightData.isTaskModel = 0;

        //构造敌人
        var mapconf = _NPCMAP[window.fightData.chapter];

        //计算等级
        var armyLv = mapconf.lv;

        var dz = mapconf.elevel;

        if(_MCFC.areaConfig[areaname]){
            window._CHESSFIGHT.defaultMap = "地形_"+areaname;
        }else{
            window._CHESSFIGHT.defaultMap = "地形_野区土";
        }
    
        //构造敌人
        window.fightData.armyTeam=[];

        var randomteam = _MRMN.randomTeam(window.fightData.chapter);
        for(var i=0;i<randomteam.length;i++){
            var tempArmyType = randomteam[i];
            window.fightData.armyTeam.push(
                _MCFC.createNpcRole(tempArmyType,{lv:armyLv,dz:dz})
            );
        }

    },

    getRandomNpc:function(mapconf){


        var npcArr = mapconf.npc;

        var randomnmb = Math.random()*100;

        for(var i=0;i<npcArr.length;i++){
            var qj = npcArr[i].per;
            if(randomnmb>qj[0]&&randomnmb<qj[1]){
                return npcArr[i].npc;
            }
        }

        return null;
    },

    getDzLevel:function(npcConf,lv){
        var per=1;
        if(npcConf.level=="D"){
            per=1;
        }else if(npcConf.level=="C"){
            per=1;
        }else if(npcConf.level=="B"){
            per=2;
        }else if(npcConf.level=="A"){
            per=3;
        }else if(npcConf.level=="S"){
            per=4;
        }else if(npcConf.level=="SS"){
            per=5;
        }
        var dzlv = Math.floor(lv/1000*50);

        var  zm = _MCH.getHardType();

        return (dzlv+zm*10)*per;
    },

     //构造敌人
     createNpcRole:function(npc,param){

        var npcConf = _NPC[npc];
        if(npcConf){
            var job = npcConf.job;
            var lv = param.lv?param.lv:5;

            var nozm = param.nozm;//是否受周目影响
            //var dz = param.dz;

            if(!nozm&&_MCH.getHardType()>0){
                lv=1000;
            }

            //boss修正
            if(npcConf.isboss){
                lv = Math.floor(lv*1.1)+5;
                if(lv>1000){
                    lv=1000;
                }
            }

            var dz = _MCFC.getDzLevel(npcConf,lv);
            if(param&&param.nodz){
                dz=0;
            }

            //console.log(lv,dz);
            var npcqua = npcConf.level;
            var tmpnpc = _CU.cloneObj(npcConf);
                tmpnpc.npctype = npc;

            tmpnpc.equip = [];

            tmpnpc.xw =  _CROF.initXw(npcConf.npcxw,lv);
                
           //根据职业和实际修为获取装备
            var equipArr = _MCFC.getEquipByJobAndRole(job,lv);
            if(equipArr&&equipArr.length>0){
                for(var i=0;i<equipArr.length;i++){
                    if(_MCH.getHardType()>0){
                        npcqua="SS";
                    }
                    tmpnpc.equip.push(_MCFC.createEquipByTypeAndDzAndRole(equipArr[i],dz,npcqua,tmpnpc));
                }
            }

            //周目强化技能
            if(!tmpnpc.skill){
                tmpnpc.skill=[];
            }
            
            /*
            if(_MCH.getHardSkill()){
                tmpnpc.skill.push(_MCH.getHardSkill());
            }*/

            tmpnpc.exp = _EXPMANAGER.getExpByLevel(lv);
            var npcdata = _ROLEDATA.getRoleDataOfPlayer(tmpnpc);
            tmpnpc.hp = npcdata.maxHp;
            tmpnpc.mg = npcdata.maxMagic;

            return tmpnpc;
        }

        return null;

    },

    //获取装备等级
    getElv:function(lv){

        var elv = 5;

        for(var i=0;i<_MCFC._ELV.length;i++){

            if(lv>=_MCFC._ELV[i]){
                elv = _MCFC._ELV[i];
            }else{
                break;
            }

        }

        return elv;

    },

    //根据职业取装备
    getEquipByJobAndRole:function(job,lv){
        var elv = _MCFC.getElv(lv);
        var tmap = {};
        var jobConfig = _JOB[job];
        if(jobConfig&&jobConfig.equipType){
            
            var resultArr = [];
            for(var key in jobConfig.equipType){
                var equip = _MCFC.getEpByTypeAndLv(elv,jobConfig.equipType[key]);
                //console.log(elv,jobConfig.equipType[key],equip);
                if(!tmap[equip]){
                    tmap[equip]=1;
                    resultArr.push(equip);
                }
            }

            return resultArr;
        }
        return null;
    },

    getEpByTypeAndLv:function(elv,type){

        var ekey = elv+"-"+type;
        if(_MCFC._EM[ekey]){
            return _MCFC._EM[ekey];
        }else{
            for(var key in _EQUIP){

                var econfig = _EQUIP[key];
                if(econfig.type==type&&econfig.lv == elv&&!econfig.iszs){
                    _MCFC._EM[ekey] = key;
                    return _MCFC._EM[ekey];
                }

            }
        }

        return null;
    },

    //构造装备
    createEquipByTypeAndDz(equipInfo,dz,qua){

        var tempequip = _IOF.createEquip(equipInfo,null);
        tempequip.qua = qua;
        var temparr = _MCFC.initEquipXw(tempequip);
        if(temparr&&temparr.length>0){
            for(var i=0;i<dz;i++){
                var tempxw = temparr[Math.floor(Math.random()*temparr.length)];
                _MDZ.realdz(tempequip,tempxw);
            }
        }
        return tempequip;
    },

    createEquipByTypeAndDzAndRole(equipInfo,dz,qua,role){

        var tempequip = _IOF.createEquip(equipInfo,null);
        tempequip.qua = qua;
        var temparr = _MCFC.initEuipXwByRole(tempequip,role);
        if(temparr&&temparr.length>0){
            var xwindex = 0;
            for(var i=0;i<dz;i++){
                var tempxw = temparr[xwindex];
                _MDZ.realdz(tempequip,tempxw);
                xwindex++;
                if(xwindex>=temparr.length){
                    xwindex=0;
                }
            }
        }
        return tempequip;
    },

    initEuipXwByRole:function(tempequip,role){

        var xwmap = _NPC[role.npctype].npcxw;
        var equipxw = {};
        var resultarr = [];
        for(var xw in xwmap){
            resultarr.push(xw);
            equipxw[xw]=0;
        }
        tempequip.xw=equipxw;
        return resultarr;
    },

    initEquipXw:function(equipInfo){

        //console.log(equipInfo);

        if(!equipInfo.qua
            ||!equipInfo.type
        ){
            return ;
        }

        var xwnmb = _CS.qua[equipInfo.qua].xwnmb;
        var etype = equipInfo.type;
        var econfig = _EQUIP[etype];
        var xwtype = econfig.xwtype;
        var resultarr = [];
        
        if(xwnmb){

            var xwmap = {};

            //先随机一条独占修为
            xwmap[xwtype]=0
            
            for(var i=1;i<xwnmb;i++){
                xwmap[XWH.getRandomXwByElv(i)]=0;
            }

            equipInfo.xw = xwmap;

            for(var key in xwmap){
                resultarr.push(key);
            }
        }

        return resultarr;
    },

    getScoreOfArmy:function(obj){
        var objdata = _ROLEDATA.getRoleDataOfPlayer(obj);
        var score= objdata.attack+objdata.defend+objdata.mattack+objdata.mdefend+Math.floor(objdata.maxHp/10);
        return {s:score,h:objdata.maxHp};
    },

    testAll:function(){

        for(var map in _NPCMAP){
            for(var i=0;i<_NPCMAP[map].npc.length;i++){
                var tempArmyType = _NPCMAP[map].npc[i].npc;
                var armyLv = _NPCMAP[map].lv;
                var dz =  _NPCMAP[map].elevel;
                var armyObj = _MCFC.createNpcRole(tempArmyType,{lv:armyLv,dz:dz});
                var scoreObj = _MCFC.getScoreOfArmy(armyObj);
                console.log(tempArmyType,armyLv,_CU.getUnitNmb(scoreObj.s),scoreObj.h,armyObj);
            }
        }

    }

}